package lanqiao;

public class quick_sort {
    // 主排序函数
    public static void quickSort(int[] array, int low, int high) {
        if (low < high) {
            int pivotIndex = partition(array, low, high);
            quickSort(array, low, pivotIndex - 1);
            quickSort(array, pivotIndex + 1, high);
        }
    }

    // 分区函数
    public static int partition(int[] array, int low, int high) {
        int pivot = array[high];
        int i = low - 1;
        System.out.println("low=" + low + " " + "high=" + (high - 1));
        for (int j = low; j < high; j++) {
            System.out.print(array[j] + " ");
            if (array[j] <= pivot) {
                i++;
                // 交换元素
                int temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
        System.out.println("\n" + "pivot=" + pivot);
        // 将基准元素放到正确位置
        int temp = array[i + 1];
        array[i + 1] = array[high];
        array[high] = temp;

        return i + 1;
    }

    // 打印数组函数
    public static void printArray(int[] array) {
        for (int i : array) {
            System.out.print(i + " ");
        }
        System.out.println();
    }

    // 主函数
    public static void main(String[] args) {
        int[] array = {23,13,35,6,19,50,28};
        System.out.println("未排序数组:");
        printArray(array);

        quickSort(array, 0, array.length - 1);

        System.out.println("已排序数组:");
        printArray(array);
    }
}
